﻿using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Susha.Models.Helpers
{
    public static class CommonTools
    {
        public static readonly String PATH_PROJECT;
        public static readonly String PATH_PHOTOS;

        public const String COOKIE_NAME = "__auth_cookie";
        public const Int32  COMMENTS_PER_GUEST_PAGE = 5;
        public const Int32  NEWS_FOR_MAIN_COUNT = 3;
        public const Int32  NEWS_FOR_NEWS_COLUMN = 5;

        static CommonTools()
        {
            PATH_PROJECT = AppDomain.CurrentDomain.BaseDirectory;
            PATH_PHOTOS = String.Format("{0}{1}", PATH_PROJECT, "Content\\i\\");
        }

        public static Tuple<String, String> GetCaptcha()
        {
            Tuple<String, String> result;

            Random random = new Random();
            Int32 randomResult = random.Next(10);


            // First value is a mix of Cyrillic and Latin letters, will be shown on page.
            // Second value Consists only of Cyrrillics. Expected from user.
            // Very simple isn't it? No one spam message since one year ago.
            switch (randomResult)
            {
                case 0:
                    result = Tuple.Create<String, String>("Mope", "Море");
                    break;
                case 1:
                    result = Tuple.Create<String, String>("Poллы", "Роллы");
                    break;
                case 2:
                    result = Tuple.Create<String, String>("Cyши", "Суши");
                    break;
                case 3:
                    result = Tuple.Create<String, String>("Kpым", "Крым");
                    break;
                case 4:
                    result = Tuple.Create<String, String>("Ялтa", "Ялта");
                    break;
                case 5:
                    result = Tuple.Create<String, String>("Bкycнo", "Вкусно");
                    break;
                case 6:
                    result = Tuple.Create<String, String>("Гopы", "Горы");
                    break;
                case 7:
                    result = Tuple.Create<String, String>("Oтдых", "Отдых");
                    break;
                case 8:
                    result = Tuple.Create<String, String>("Kypopт", "Курорт");
                    break;
                default:
                    result = Tuple.Create<String, String>("Cyшa", "Суша");
                    break;
            }

            return result;
        }

        public static void ResetDB(Boolean recreateTables)
        {
            Configuration cfg = new Configuration();
            cfg.Configure();
            cfg.AddAssembly(typeof(Category).Assembly);
            if (recreateTables)
            {
                new SchemaExport(cfg).Execute(true, true, false);

                Category c1 = new Category() { Name = "Sashimi" };
                Category c2 = new Category() { Name = "Sushami" };
                Category c3 = new Category() { Name = "Rolls" };
                Sushi s1 = new Sushi() { Name = "Dragon", ParentCategory = c1, Price = 2, SmallPhoto = "001.jpg" };
                Sushi s2 = new Sushi() { Name = "Dragun", ParentCategory = c1, Price = 2, SmallPhoto = "002.jpg" };
                Sushi s3 = new Sushi() { Name = "Drogon", ParentCategory = c1, Price = 2, SmallPhoto = "003.jpg" };
                Sushi s4 = new Sushi() { Name = "Dragun2", ParentCategory = c2, Price = 2, SmallPhoto = "004.jpg" };
                Sushi s5 = new Sushi() { Name = "Dragon2", ParentCategory = c2, Price = 2, SmallPhoto = "005.jpg" };
                Sushi s6 = new Sushi() { Name = "Dragun3", ParentCategory = c3, Price = 2, SmallPhoto = "006.jpg" };
                UserClass u1 = new UserClass() { Email = "admin", Password = "123", Role = "Admin" };

                CategoryRepository.Save(c1);
                CategoryRepository.Save(c2);
                CategoryRepository.Save(c3);
                SushiRepository.Save(s1);
                SushiRepository.Save(s2);
                SushiRepository.Save(s3);
                SushiRepository.Save(s4);
                SushiRepository.Save(s5);
                SushiRepository.Save(s6);
                SushiRepository.Save(s1);
                SushiRepository.Save(s2);
                SushiRepository.Save(s3);
                SushiRepository.Save(s4);
                SushiRepository.Save(s5);
                SushiRepository.Save(s6);
                UserRepository.Save(u1);
            }
            else
            {
                new SchemaUpdate(cfg).Execute(false, true);
            }
        }
    }
}